package xiyuan.view.json;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import xiyuan.view.data.WeiboUser;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
	private JsonGenerator jsonGenerator = null;
	private ObjectMapper objectMapper = null;
	private WeiboUser bean = null;

	public static void main(String[] agrs){
	}
	
	public void init(OutputStream out) {
		objectMapper = new ObjectMapper();
		try {
			jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(
					out, JsonEncoding.UTF8);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void destory() {
		try {
			if (jsonGenerator != null) {
				jsonGenerator.flush();
				if (!jsonGenerator.isClosed())
					jsonGenerator.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jsonGenerator = null;
		objectMapper = null;
		bean = null;
		System.gc();
	}

	public void getUserList(List<WeiboUser> users) {
		try {
			jsonGenerator.writeObject(users);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return jsonArray;
	}
}
